package es.ats.codington.spring.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.LargeBusiness;
import es.ats.codington.festival.domain.Museum;
import es.ats.codington.festival.domain.Park;
import es.ats.codington.festival.domain.Stadium;
import es.ats.codington.festival.domain.Theater;
import es.ats.codington.festival.domain.TouristAttraction;
import es.ats.codington.festival.domain.TraditionalMarket;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.domain.Zoo;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.EventSignUpService;

/**
 * Events Controller
 * 
 */
@Controller
public class EventsController{
	
	/**
	 * This controller receives the session created by visitor 
	 * and sends the event list of the visitor to ModelAndView
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/vieweventsofuser.htm")
	public ModelAndView listMyEventsController(HttpServletRequest request, HttpServletResponse response) throws SQLException {
		
		ModelAndView modelAndView = new ModelAndView();
		
		HttpSession session=request.getSession();
		Visitor visitor = (Visitor) session.getAttribute("visitor");
		
		EventSignUp e = new EventSignUp();
		e.setIdVisitor(visitor.getIdVisitor());
		EventSignUpService es = new EventSignUpService();
		List<Event> eventList = null;
		eventList = es.viewEventsOfUser(e);
		modelAndView.addObject("eventSignUpList",eventList);
		modelAndView.setViewName("NewCodington_myPortal");
		
		return modelAndView;
	}	
	
	/**
	 * This controller sends all the event list to ModelAndView
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/ShowAllEvents.htm")
	public ModelAndView showAllEventsController(HttpServletRequest request, HttpServletResponse response) throws SQLException {
		
		ModelAndView modelAndView = new ModelAndView();
		
		ArrayList<Event> eventList = null;
		EventService es=new EventService();
		try{
		eventList = es.getShowEvents();
		
		modelAndView.addObject("eventList",eventList);
		modelAndView.setViewName("NewCodington_showEvents");
			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return modelAndView;
	}
	
	/**
	 * This controller receives the place selected
	 * and sends the event list of the place to ModelAndView
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/ShowEventsByPlace.htm")
	public ModelAndView showEventsByPlaceController(HttpServletRequest request, HttpServletResponse response) throws SQLException {
		
	ModelAndView modelAndView = new ModelAndView();
	
	EventService es=new EventService();
	ArrayList<Event> eventList = null;
	
	try {
		Event event=new Event();
		event.setPlace(request.getParameter("place"));
		request.getSession().setAttribute("place", event.getPlace());

		eventList = es.getShowEventsByPlace(event);
	 	modelAndView.addObject("eventByPlace",eventList);
	 	
	 	int id=0;
	 	
	 	
	 	id=es.getIdPlace(event.getPlace());
	 	
	 	 			
	 	
	 	switch(id){
	 	
	 	case 1:
	 		Stadium stadium =new Stadium();
	 		stadium.setIdPlace(id);
	 		stadium=es.showStadium(stadium);
	 		
	 		request.setAttribute("place_placeName",stadium.getPlaceName());
	 		request.setAttribute("place_zone",stadium.getZone());
	 		request.setAttribute("place_capacity",stadium.getCapacity());
	 		request.setAttribute("place_name",stadium.getStadiumName());
	 		break;
	 	case 2:
	 		Theater theater =new Theater();
	 		theater.setIdPlace(id);
	 		theater=es.showTheater(theater);
	 		
	 		request.setAttribute("place_placeName",theater.getPlaceName());
	 		request.setAttribute("place_zone",theater.getZone());
	 		request.setAttribute("place_capacity",theater.getCapacity());
	 		request.setAttribute("place_name",theater.getTheaterName());
	 		break;
	 	case 3:
	 		Museum museum =new Museum();
	 		museum.setIdPlace(id);
	 		museum=es.showMuseum(museum);
	 		
	 		request.setAttribute("place_placeName",museum.getPlaceName());
	 		request.setAttribute("place_zone",museum.getZone());
	 		request.setAttribute("place_capacity",museum.getCapacity());
	 		request.setAttribute("place_name",museum.getMuseumName());
	 		break;
	 	case 4:
	 		LargeBusiness largebusiness =new LargeBusiness();
	 		largebusiness.setIdPlace(id);
	 		largebusiness=es.showLargeBusiness(largebusiness);
	 		
	 		request.setAttribute("place_placeName",largebusiness.getPlaceName());
	 		request.setAttribute("place_zone",largebusiness.getZone());
	 		request.setAttribute("place_capacity",largebusiness.getCapacity());
	 		request.setAttribute("place_name",largebusiness.getLargeBusinessName());
	 		break;
	 	case 5:
	 		Zoo zoo =new Zoo();
	 		zoo.setIdPlace(id);
	 		zoo=es.showZoo(zoo);
	 		
	 		request.setAttribute("place_placeName",zoo.getPlaceName());
	 		request.setAttribute("place_zone",zoo.getZone());
	 		request.setAttribute("place_capacity",0);
	 		request.setAttribute("place_name",zoo.getZooName());
	 		break;
	 	case 6:
	 		Park park =new Park();
	 		park.setIdPlace(id);
	 		park=es.showPark(park);
	 		
	 		request.setAttribute("place_placeName",park.getPlaceName());
	 		request.setAttribute("place_zone",park.getZone());
	 		request.setAttribute("place_capacity",0);
	 		request.setAttribute("place_name",park.getparkName());
	 		break;
	 	case 7:
	 		TraditionalMarket traditionalMarket =new TraditionalMarket();
	 		traditionalMarket.setIdPlace(id);
	 		traditionalMarket=es.showTraditionalMarket(traditionalMarket);
	 		
	 		request.setAttribute("place_placeName",traditionalMarket.getPlaceName());
	 		request.setAttribute("place_zone",traditionalMarket.getZone());
	 		request.setAttribute("place_capacity",0);
	 		request.setAttribute("place_name",traditionalMarket.getMarketName());
	 		break;
	 	case 8:
	 		TouristAttraction touristAttraction =new TouristAttraction();
	 		touristAttraction.setIdPlace(id);
	 		touristAttraction=es.showTouristAttraction(touristAttraction);
	 		
	 		request.setAttribute("place_placeName",touristAttraction.getPlaceName());
	 		request.setAttribute("place_zone",touristAttraction.getZone());
	 		request.setAttribute("place_capacity",0);
	 		request.setAttribute("place_name",touristAttraction.getAttractionName());
	 		break;
	 	}
	 	
	 	
	 	modelAndView.setViewName("NewCodington_showEventsByPlace");  
		
	} catch (SQLException e) {
		e.printStackTrace();
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
	return modelAndView;
}
	
	/**
	 * This controller updates the page where shows event
	 * list of the place with error derived of register to event
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/ShowEventsByPlaceError.htm")
	public ModelAndView showEventsByPlaceErrorController(HttpServletRequest request, HttpServletResponse response) throws SQLException {
		
	ModelAndView modelAndView = new ModelAndView();
	
	EventService es=new EventService();
	ArrayList<Event> eventList = null;
	
	try {
		Event event=new Event();
		
		event.setPlace((String)request.getSession().getAttribute("place"));
		request.getSession().setAttribute("place", event.getPlace());

		eventList = es.getShowEventsByPlace(event);
	 	modelAndView.addObject("eventByPlace",eventList);
	 	
	 	int id=0;
	 		 		
	 	id=es.getIdPlace(event.getPlace());
	 	
	 	switch(id){
	 	
	 	case 1:
	 		Stadium stadium =new Stadium();
	 		stadium.setIdPlace(id);
	 		stadium=es.showStadium(stadium);
	 		
	 		request.setAttribute("place_placeName",stadium.getPlaceName());
	 		request.setAttribute("place_zone",stadium.getZone());
	 		request.setAttribute("place_capacity",stadium.getCapacity());
	 		request.setAttribute("place_name",stadium.getStadiumName());
	 		break;
	 	case 2:
	 		Theater theater =new Theater();
	 		theater.setIdPlace(id);
	 		theater=es.showTheater(theater);
	 		
	 		request.setAttribute("place_placeName",theater.getPlaceName());
	 		request.setAttribute("place_zone",theater.getZone());
	 		request.setAttribute("place_capacity",theater.getCapacity());
	 		request.setAttribute("place_name",theater.getTheaterName());
	 		break;
	 	case 3:
	 		Museum museum =new Museum();
	 		museum.setIdPlace(id);
	 		museum=es.showMuseum(museum);
	 		
	 		request.setAttribute("place_placeName",museum.getPlaceName());
	 		request.setAttribute("place_zone",museum.getZone());
	 		request.setAttribute("place_capacity",museum.getCapacity());
	 		request.setAttribute("place_name",museum.getMuseumName());
	 		break;
	 	case 4:
	 		LargeBusiness largebusiness =new LargeBusiness();
	 		largebusiness.setIdPlace(id);
	 		largebusiness=es.showLargeBusiness(largebusiness);
	 		
	 		request.setAttribute("place_placeName",largebusiness.getPlaceName());
	 		request.setAttribute("place_zone",largebusiness.getZone());
	 		request.setAttribute("place_capacity",largebusiness.getCapacity());
	 		request.setAttribute("place_name",largebusiness.getLargeBusinessName());
	 		break;
	 	case 5:
	 		Zoo zoo =new Zoo();
	 		zoo.setIdPlace(id);
	 		zoo=es.showZoo(zoo);
	 		
	 		request.setAttribute("place_placeName",zoo.getPlaceName());
	 		request.setAttribute("place_zone",zoo.getZone());
	 		request.setAttribute("place_capacity",0);
	 		request.setAttribute("place_name",zoo.getZooName());
	 		break;
	 	case 6:
	 		Park park =new Park();
	 		park.setIdPlace(id);
	 		park=es.showPark(park);
	 		
	 		request.setAttribute("place_placeName",park.getPlaceName());
	 		request.setAttribute("place_zone",park.getZone());
	 		request.setAttribute("place_capacity",0);
	 		request.setAttribute("place_name",park.getparkName());
	 		break;
	 	case 7:
	 		TraditionalMarket traditionalMarket =new TraditionalMarket();
	 		traditionalMarket.setIdPlace(id);
	 		traditionalMarket=es.showTraditionalMarket(traditionalMarket);
	 		
	 		request.setAttribute("place_placeName",traditionalMarket.getPlaceName());
	 		request.setAttribute("place_zone",traditionalMarket.getZone());
	 		request.setAttribute("place_capacity",0);
	 		request.setAttribute("place_name",traditionalMarket.getMarketName());
	 		break;
	 	case 8:
	 		TouristAttraction touristAttraction =new TouristAttraction();
	 		touristAttraction.setIdPlace(id);
	 		touristAttraction=es.showTouristAttraction(touristAttraction);
	 		
	 		request.setAttribute("place_placeName",touristAttraction.getPlaceName());
	 		request.setAttribute("place_zone",touristAttraction.getZone());
	 		request.setAttribute("place_capacity",0);
	 		request.setAttribute("place_name",touristAttraction.getAttractionName());
	 		break;
	 	}
	 			 	
	 	modelAndView.setViewName("NewCodington_showEventsByPlace");  
		
	} catch (SQLException e) {
		e.printStackTrace();
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
	return modelAndView;
	}	
		
	@RequestMapping(value = "/EventRegistrationPage.htm")
	public String eventRegistrationPageController() {
		return "NewCodington_eventRegistration";
	}
	
	@RequestMapping(value = "/EventRegistration.htm")
	public String validaterRegisterController(HttpServletRequest request, HttpServletResponse response) throws IOException {

		Event event=new Event();
		
		event.setEventName(request.getParameter("eventname"));  
		event.setDescription(request.getParameter("description")); 
		event.setPlace(request.getParameter("place"));  
		event.setDuration(Integer.parseInt(request.getParameter("duration")));
		event.setEventType(request.getParameter("eventtype"));  
		event.setSeatsTotal(Integer.parseInt(request.getParameter("totaltickets")));
		event.setSeatsAvailable(Integer.parseInt(request.getParameter("totaltickets")));   

		EventService es=new EventService();	
		    
		    try {
		    	//if(es.checkEvent(event)==false){
		    	
		    	if(es.registerNewEvent(event)==1){
		    		
		    		request.setAttribute("error", "Event registered");
		    		
		    		return "NewCodington_eventRegistration";
		    			
		    	}
		    	else{  
		    		request.setAttribute("error", "Event NOT registered");
		    		
		    		return "NewCodington_eventRegistration";	
		    	} 
		    	/*
		    	else{
	    		session.setAttribute("error", "This event exists");
	    		 
	    		return "NewCodington_eventRegistration";
	    	}
		    	 */
		    }
		    catch(Exception eee){
		    	return eee.getMessage();
		    }	    
	}
	
}